package com.dhcc.DRGsHosp.pdfRender.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @title		:GetAbnormity.java
 * @description	:检测异常数据
 * @author		:liuhongjun
 * @date		:2016年7月27日
 * @version     :2.2.2
 * @copyright   :DHCC 2014-2016
 */
public class GetAbnormity {
	/**
	 * 
	 * @methodname    :getAverage
	 * @description   : 计算数列平均值
	 * @param         :
	 * @author        :liuhongjun
	 * @return        :
	 * @date          :2016年7月27日
	 */
	  public double getAverage(List<Double> list ){
		  double sum=0;
		for(int i = 0;i < list.size();i++){
		   sum += list.get(i);
		}
		return (double)(sum / list.size());
		}

		/**
		 * @methodname    :getStandardDevition
		 * @description   : 计算标准差
		 * @param         :
		 * @author        :liuhongjun
		 * @return        :
		 * @date          :2016年7月27日
		 */
		public double getStandardDevition(List<Double> list ){
		double sum = 0;
		for(int i = 0;i < list.size();i++){
		sum += Math.sqrt(((double)list.get(i)-getAverage(list)) * (list.get(i) -getAverage(list)));
		}
		return (sum / (list.size() - 1));
		}
		List<Map<String, Object>> mainOperTop5MapList = new ArrayList<Map<String, Object>>();

		/**
		 * @methodname    :getAbnormalDataObj
		 * @description   : 抛出异常数据
		 * @param         :
		 * @author        :liuhongjun
		 * @return        :
		 * @date          :2016年7月27日
		 */
		public Map<String, List<Double>> getAbnormalDataObj(List<Double> list ){	
			Map<String,List<Double> > map = new HashMap<String, List<Double>>();
			List<Double> norList = new ArrayList<Double>();
			List<Double> abnorList = new ArrayList<Double>();
			double avgdata=getAverage(list);
			double s=getAverage(list);
			double lndata=Math.log(list.size());
			double w=1+0.4*lndata;
			for(int i=0;i<list.size();i++){
				double jiance=list.get(i)-avgdata;
					if(jiance<0){
					jiance=-jiance;
					}
					if(jiance>w*s){
						abnorList.add(list.get(i));
						System.out.println("异常"+list.get(i));
					}else{
						 norList.add(list.get(i));
						System.out.println("非异常"+list.get(i));
					}
			}
			map.put("正常", norList);
			map.put("异常", abnorList);
			return map;
		}
	/**
	 * @methodname    :getAbnormalDataMap
	 * @description   : 
	 * @param         :
	 * @author        :liuhongjun
	 * @return        :
	 * @date          :2016年7月27日
	 */
	 public Map<String, List <Map<String, Object>>> getAbnormalDataMap(List <Map<String, Object>> mapList,String keyValue1,String keyValue2){	
			Map<String,List <Map<String, Object>>> map = new HashMap<String,List <Map<String, Object>>>();
			List<Double> list1 = new ArrayList<Double>();
			List<Double> list2 = new ArrayList<Double>();
			List <Map<String, Object>> norMapList=new ArrayList <Map<String, Object>>();				
			List <Map<String, Object>> abnorMapList=new ArrayList <Map<String, Object>>();
			
			for(int j=0;j<mapList.size();j++){
				list1.add(Double.valueOf(mapList.get(j).get(keyValue1)==null?"0":mapList.get(j).get(keyValue1).toString()));
			}
			double avgdata1=getAverage(list1);
			double s1=getAverage(list1);
			double lndata1=Math.log(list1.size());
			double w1=1+0.4*lndata1;
			
			for(int j=0;j<mapList.size();j++){
				list2.add(Double.valueOf(mapList.get(j).get(keyValue2)==null?"0":mapList.get(j).get(keyValue2).toString()));
			}
			double avgdata2=getAverage(list2);
			double s2=getAverage(list2);
			double lndata2=Math.log(list2.size());
			double w2=1+0.4*lndata2;
			
		  for(int i=0;i<mapList.size();i++){
				double jiance1=Double.valueOf(mapList.get(i).get(keyValue1)==null?"0":mapList.get(i).get(keyValue1).toString())-avgdata1;
				double jiance2=Double.valueOf(mapList.get(i).get(keyValue2)==null?"0":mapList.get(i).get(keyValue2).toString())-avgdata2;
					if(jiance1<0){
					jiance1=-jiance1;
					}
					if(jiance2<0){
						jiance2=-jiance2;
						}
					if(jiance1<=w1*s1 && jiance2<=w2*s2 ){
						norMapList.add(mapList.get(i));
						
					}else{
						 abnorMapList.add(mapList.get(i));
					}
			}
			map.put("norMapList", norMapList);
			map.put("abnorMapList", abnorMapList);
			return map;
		}
		
}
